Skip to content

fix: don't process Value names for backslash escapes in re.sub - #149

Merged
harro merged 2 commits into
google:masterfrom
rahul05ranjan:fix/value-name-resub
Nov 16, 2026
Merged

harro merged 2 commits into
google:masterfrom
rahul05ranjan:fix/value-name-resub

Conversation

@rahul05ranjan

Copy link
Copy Markdown
Contributor

Fixes #148

Problem

A backslash in a Value name made TextFSMValue.Parse raise a raw re.PatternError — unwrapped — because the name is interpolated into an re.sub() replacement string ((?P<%s> ...), which re.sub processes for backslash escapes (it would also expand group references such as \g<1> in a name). Every other malformed template raises TextFSMTemplateError; the Value's own regex compilation is even wrapped with except re.error: raise TextFSMTemplateError(...) — this re.sub just bypassed that wrapping.

Fix

Use a callable replacement so the name is inserted literally:

self.template = re.sub(r'^\(', lambda match: '(?P<%s>' % self.name, self.regex)

Behavior for valid names is unchanged. A name that isn't a valid group name now surfaces later as the existing TextFSMTemplateError ("Invalid regular expression: ...") when the value is referenced in a rule, consistent with all other template errors.

Testing

Added to tests/textfsm_test.py (testFSMValue):

  • Value beer\ol1 (\S+) now parses with template == r'(?P<beer\ol1>\S+)' — fails on main with the raw re.PatternError: bad escape \o at position 8, passes with this fix.
  • A full template using such a value raises TextFSMTemplateError from TextFSM, not a raw exception.

Full suite: python3 -m unittest tests.textfsm_test tests.clitable_test tests.terminal_test tests.texttable_test → Ran 108 tests ... OK (no failures before or after; the new assertions live inside the existing testFSMValue).

A backslash in a Value name escaped the re.sub() replacement as a raw
re.PatternError (e.g. 'Value beer\ol1 (\S+)'), instead of a
TextFSMTemplateError when the value is used. A callable replacement
keeps the name literal.

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
Comment thread textfsm/parser.py Resolved
Comment thread textfsm/parser.py Resolved
Comment thread tests/textfsm_test.py Resolved
Per review: a backslash in a Value name is now rejected at name-check
time, and the template is built by slicing the already-validated leading
'(' instead of re.sub() with a callable replacement. Tests trimmed to
the name-check error and the full-template case.

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Thanks for the review! All suggestions applied in af91364.

  • parser.py:324 — Done: self.template = '(?P<%s>%s' % (self.name, self.regex[1:]). Verified it produces output identical to the previous re.sub() for ordinary names (differential check across valid Value regexes), since the leading ( is already validated.
  • parser.py:303 — Done: the check is now if len(self.name) > self.max_name_len or '\\' in self.name:.
  • tests/textfsm_test.py:91 — Done: kept line 91 onward (the full-template TextFSM case) and replaced the block before it with a direct assertion that Value beer\ol1 (\S+) raises TextFSMTemplateError at name-check time.

Suite: 108 passed, 28 subtests passed (pytest -o addopts="" -o filterwarnings=ignore).

@harro
harro self-requested a review November 16, 2026 02:48
@harro
harro merged commit 37c20df into google:master Nov 16, 2026
6 checks passed
@rahul05ranjan

Copy link
Copy Markdown
Contributor Author

Thank you for the careful review and the merge @harro! The template-construction suggestion made the change much cleaner.

On a personal note — I'm currently open to new opportunities (Full Stack (PHP, NodeJs, Python) / Data Science / Applied AI / Devtools, based in India). If there's an opening or referral you'd recommend, my inbox is open:
LinkedIn: https://www.linkedin.com/in/rahulswt7/ · GitHub: https://github.com/rahul05ranjan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Backslash in Value name raises raw re.PatternError instead of TextFSMTemplateError

2 participants